package com.jjh.web.controller;

import com.jjh.winterframework.annatation.Autowired;
import com.jjh.winterframework.annatation.Component;
import com.jjh.pojo.Shop;
import com.jjh.service.DishService;
import com.jjh.service.ShopService;
import com.jjh.service.impl.DishServiceImpl;
import com.jjh.service.impl.ShopServiceImpl;
import com.jjh.util.PageBody;
import com.jjh.web.Menu;
import com.jjh.web.view.DishView;
import com.jjh.web.view.ShopView;


import java.util.List;
import java.util.Scanner;
@Component
public class ShopController implements ShopView {

    private static Scanner scanner = new Scanner(System.in);
    @Autowired
    private DishController dishView;
    @Autowired
    private ShopServiceImpl shopService ;
    /**
     * 调用菜品的服务
     */
    @Autowired
    private DishServiceImpl dishService;





    /**
     * 展示商家信息
     */
    @Override
    public void showShop() {


        System.out.println("<===================================== 商 家 列 表 ===================" +
                "=====================>");
        System.out.println("  -> 输入 序号 选择商家");
        System.out.println("  -> 输入 0 返回上一级菜单");
        System.out.println("  -> 输入 up/down 来切换页面");
        System.out.println();

        PageBody pagebody = shopService.page(1, 10);

        int currntPage = 1;
        int size = pagebody.getSize();
        int pageSize = pagebody.getPageSize();
        int total = pagebody.getTotal();
        List<Object> shopList = pagebody.getResults();
        int i = 1;
        System.out.println("序 号           名 称           星 数 ");
        for (Object o : shopList) {
            Shop shop = (Shop) o;
            System.out.print(" "+i + "    \t\t" + shop.getName() + "     \t\t" + shop.getStar()+"\r\n\r\n");
            i++;
        }
        System.out.println("当前第 " + currntPage + "/" + pageSize + " 页 " + "     一共: " + total + " 条数据");

        boolean flag = true;
        while (flag) {
            System.out.print(" --> 请输入值:");
            String code = scanner.nextLine();
            switch (code) {
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case "10":
                    dishView.showDishes((Shop) shopList.get(Integer.parseInt(code) - 1));
                    flag = false;
                    break;
                case "up":
                    --currntPage;
                    if (currntPage < 1) {
                        currntPage = 1;
                        System.out.println("=====>当前已经是第一页!\r\n");
                        break;
                    } else if (currntPage > pageSize) {
                        System.out.println("=====>当前已经是最后一页!\r\n");
                        currntPage = pageSize;
                        break;
                    }
                    changePage(currntPage, size);
                    break;
                case "down":
                    ++currntPage;
                    if (currntPage < 1) {
                        currntPage = 1;
                        System.out.println("=====>当前已经是第一页!\r\n");
                        break;
                    } else if (currntPage > pageSize) {
                        System.out.println("=====>当前已经是最后一页!\r\n");
                        currntPage = pageSize;
                        break;
                    }
                    changePage(currntPage, size);
                    break;
                case "0":
                    Menu.subMenu01();
                    flag = false;
                    break;
                default:
                    System.out.println("=====>出错了! 请重新输入:");
                    break;
            }
        }

    }


        /**
         * 跟换页面
         */
        @Override
        public void changePage(int page, int size) {
            PageBody pagebody = shopService.page(page, size);
            int currntPage = pagebody.getCurrntPage();
            int pageSize = pagebody.getPageSize();
            int total = pagebody.getTotal();
            List<Object> shopList = pagebody.getResults();
            int i = 1;
            System.out.println("序 号           名 称           星 数 ");
            for (Object o : shopList) {
                Shop shop =(Shop) o;
                System.out.print(" "+i + "    \t\t" + shop.getName() + "     \t\t" + shop.getStar()+"\r\n\r\n");
                i++;
            }
            System.out.println("当前第 " + currntPage + "/" + pageSize + " 页 " + "     一共: " + total + " 条数据");

        }





    }

